package cn.migu.music.hangupsms.repository;

import cn.migu.music.hangupsms.common.entity.SmsSendFrequencySettings;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface SmsSendFrequencySettingsMapper {
    @Insert(value = "insert into t_migu_hangup_sms_send_frequency" +
                    "(province_id,day_frequency,week_frequency,month_frequency,change_time,apply_status,apply_time,delete_flag,create_time,update_time)" +
                    " values (#{settings.provinceId},#{settings.dayFrequency},#{settings.weekFrequency},#{settings.monthFrequency},#{settings.changeTime}" +
                    ",#{settings.applyStatus},#{settings.applyTime},0,now(),now())")
    void add(@Param("settings") SmsSendFrequencySettings settings);

    @Update(value = "<script>update t_migu_hangup_sms_send_frequency set \n" +
                    "  <if test=\"settings.dayFrequency != null\">\n" +
                    "  day_frequency=#{settings.dayFrequency},\n" +
                    "  </if>\n" +
                    "  <if test=\"settings.weekFrequency != null\">\n" +
                    "  week_frequency=#{settings.weekFrequency},\n" +
                    "  </if>\n" +
                    "  <if test=\"settings.monthFrequency != null\">\n" +
                    "  month_frequency=#{settings.monthFrequency},\n" +
                    "  </if>\n" +
                    " <if test=\"settings.changeTime != null\">\n" +
                    "  change_time=#{settings.changeTime},\n" +
                    "  </if>\n" +
                    "  <if test=\"settings.deleteFlag != null\">" +
                    "  delete_flag=#{settings.deleteFlag}," +
                    "  </if>" +
                    "  <if test=\"settings.applyStatus != null\">" +
                    "  apply_status=#{settings.applyStatus}," +
                    "  </if>" +
                    "  update_time=now()\n" +
                    " where province_id=#{settings.provinceId}</script>")
    void updateFrequency(@Param("settings") SmsSendFrequencySettings settings);

    @Update(value = "update t_migu_hangup_sms_send_frequency" +
                    " set apply_status=#{applyStatus},apply_time=#{applyTime}" +
                    " where province_id=#{provinceId}")
    void updateApplyStatus(@Param("provinceId") String provinceId,@Param("applyStatus") int applyStatus,@Param("applyTime") LocalDateTime applyTime);

    @Update(value = "update t_migu_hangup_sms_send_frequency set delete_flag = 1 where province_id=#{provinceId}")
    void delete(@Param("provinceId") String provinceId);

    @Select(value = "select" +
                    " province_id,day_frequency,week_frequency,month_frequency,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_send_frequency" +
                    " where province_id=#{provinceId} and delete_flag=0")
    SmsSendFrequencySettings get(@Param("provinceId") String provinceId);

    @Select(value = "select" +
                    " province_id,day_frequency,week_frequency,month_frequency,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_send_frequency" +
                    " where province_id=#{provinceId}")
    SmsSendFrequencySettings getIgnoreDelete(@Param("provinceId") String provinceId);

    @Select(value = "select" +
                    " province_id,day_frequency,week_frequency,month_frequency,change_time,apply_status,apply_time,delete_flag,create_time,update_time" +
                    " from t_migu_hangup_sms_send_frequency where delete_flag = 0")
    List<SmsSendFrequencySettings> getAllSettings();
}
